package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.BaseCategory1;
import com.atguigu.gmall.product.mapper.BaseCategory1Mapper;
import com.atguigu.gmall.product.service.BaseCategory1Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Queue;

@Service
public class BaseCategory1ServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1>
                                      implements BaseCategory1Service {
    /**
     * 根据id查询一级分类信息
     * @param id
     * @return
     */
    @Override
    public BaseCategory1 get(Long id) {
        //判断传入参数是否为空
        if (id == null)
            throw new RuntimeException("传入参数错误!");
        return baseMapper.selectById(id);
    }

    /**
     * 查询全部一级分类信息
     * @return
     */
    @Override
    public List<BaseCategory1> getAll() {
        return baseMapper.selectList(null);
    }

    /**
     * 新增一级分类
     * @param baseCategory1
     */
    @Override
    public void add(BaseCategory1 baseCategory1) {
        //验证新增数据是否为空
        if (baseCategory1 == null)
            throw new RuntimeException("传入参数错误!");
        baseMapper.insert(baseCategory1);
    }

    /**
     * 根据id修改一级分类信息
     * @param baseCategory1
     */
    @Override
    public void update(BaseCategory1 baseCategory1){
        //验证修改数据是否为空
        if (baseCategory1 == null)
            throw new RuntimeException("传入参数错误!");
        //修改条件
        LambdaQueryWrapper<BaseCategory1> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseCategory1::getId,baseCategory1.getId());
        baseMapper.update(baseCategory1,queryWrapper);
    }

    /**
     * 根据id删除一级分类信息
     * @param id
     */
    @Override
    public void delete(Long id) {
        //判断传入id是否为空
        if (id == null)
            throw new RuntimeException("传入参数错误!");
        baseMapper.delete(new LambdaQueryWrapper<BaseCategory1>().eq(BaseCategory1::getId,id));
    }

    /**
     * 根据条件查询一级分类
     * @param baseCategory1
     * @return
     */
    @Override
    public List<BaseCategory1> conditionSearch(BaseCategory1 baseCategory1) {
        //判断传入条件是否为空
        if(baseCategory1 == null)
            //为空则查询全部
            return this.getAll();
        //获取查询条件
        LambdaQueryWrapper queryWrapper = this.getCondition(baseCategory1);
        //执行查询
        List<BaseCategory1> baseCategory1s = baseMapper.selectList(queryWrapper);
        return baseCategory1s;
    }

    /**
     * 分页查询一级分类
     * @param page
     * @param size
     * @return
     */
    @Override
    public IPage<BaseCategory1> getPage(Integer page, Integer size) {
        if(page == null || size == null)
            throw new RuntimeException("参数错误!");
        IPage<BaseCategory1> iPage = new Page<>(page,size);
        IPage<BaseCategory1> baseCategory1IPage = baseMapper.selectPage(iPage, null);
        return baseCategory1IPage;
    }

    /**
     * 分页条检查询
     * @param baseCategory1 查询条件对象
     * @param page 当前页码
     * @param size 一页显示数量
     * @return
     */
    @Override
    public IPage<BaseCategory1> conditionSearch(BaseCategory1 baseCategory1, Integer page, Integer size) {
        if(page == null || size == null)
            throw new RuntimeException("参数错误!");
        //判断传入条件是否为空
        if(baseCategory1 == null ){
            //如果为空则查询全部
           return baseMapper.selectPage(new Page<BaseCategory1>(page,size),null);
        }
        //查询条件不为空 获取查询条件
        LambdaQueryWrapper queryWrapper = this.getCondition(baseCategory1);
        return baseMapper.selectPage(new Page<BaseCategory1>(page,size),queryWrapper);
    }

    private LambdaQueryWrapper getCondition(BaseCategory1 baseCategory1){
        //构建查询条件
        LambdaQueryWrapper<BaseCategory1> queryWrapper = new LambdaQueryWrapper<>();
        //判断传入id是否为空
        if(baseCategory1.getId() != null)
            //不为空作为查询条件
            queryWrapper.eq(BaseCategory1::getId,baseCategory1.getId());
        //判断传入名称是否为空
        if(!StringUtils.isEmpty(baseCategory1.getName()))
            //不为空作为查询条件
            queryWrapper.like(BaseCategory1::getName,baseCategory1.getName());
        return queryWrapper;
    }
}
